const fs = require('fs')
const crypto = require('crypto')
const Bluebird = require('bluebird')
const profiler = require('v8-profiler')
const Paloma = require('paloma')
const app = new Paloma()
app.route({
    method: 'GET',
    path: '/encrypt',
    controller: function encryptRouter(ctx) {
        const password = ctx.query.password || 'test'
        const salt = crypto.randomBytes(128).toString('base64')
        const encryptedPassword = crypto.pbkdf2Sync(password, salt, 10000, 64, 'sha512').toString('hex')
        ctx.body = encryptedPassword
    }
})
app.route({
    method: 'GET',
    path: '/cpuprofile',
    async controller(ctx) {
        //Start Profiling
        profiler.startProfiling('CPU profile')
        await Bluebird.delay(30000)
        //Stop Profiling after 30s
        const profile = profiler.stopProfiling()
        profile.export()
            .pipe(fs.createWriteStream(`cpuprofile-${Date.now()}.cpuprofile`))
            .on('finish', () => profile.delete())
        ctx.status = 204
    }
})
app.listen(3000)